package com.hqyj.Controller;
import com.hqyj.Pojo.User;
import com.hqyj.Service.DutyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
//接收请求，调用业务，跳转页面
public class DutyController {
    @Autowired
    DutyService dutyService;
    @RequestMapping(value = "/people.do" ,method = RequestMethod.GET)
    public String people(HttpServletRequest request){
        //获取session对象，并强制转换为user类型
        User users=(User)request.getSession().getAttribute("user");
        //取出rolrid
        int roleid=users.getRole_id();
        //执行查询业务
        List<User> userList=dutyService.queryUserByRoleId(roleid);
        //结果集返回给新的jsp
        request.setAttribute("userList",userList);
        //跳转页面
        return "People_List";
    }
    @RequestMapping(value = "/delete.do",method = RequestMethod.GET)
    public  String delete(@RequestParam("user_id")int user_id,HttpServletRequest request){
        dutyService.delete(user_id);
        //刷新界面
        User users=(User)request.getSession().getAttribute("user");
        //取出rolrid
        int roleid=users.getRole_id();
        //执行查询业务
        List<User> userList=dutyService.queryUserByRoleId(roleid);
        //结果集返回给新的jsp
        request.setAttribute("userList",userList);
        //跳转页面
        return "People_List";
    }
    @RequestMapping(value = "/update.do",method = RequestMethod.GET)
    public String update(User user, HttpServletRequest request){
        if(user.getRole_name().equals("boss")){
            user.setRole_id(1);
        }
        else if(user.getRole_name().equals("大哥")){
            user.setRole_id(2);
        }
        else if(user.getRole_name().equals("二哥")){
            user.setRole_id(3);
        }
        else if(user.getRole_name().equals("保安")){
            user.setRole_id(4);
        }
        else{
            //刷新操作
            User users1=(User)request.getSession().getAttribute("user");
            //取出rolrid
            int roleid=users1.getRole_id();
            //执行查询业务
            List<User> userList=dutyService.queryUserByRoleId(roleid);
            //结果集返回给新的jsp
            request.setAttribute("userList",userList);
            request.setAttribute("MSG","职位输入异常!!");
            //跳转页面
            return "People_List";
        }
        dutyService.update(user);
        //刷新操作
        User users1=(User)request.getSession().getAttribute("user");
        //取出rolrid
        int roleid=users1.getRole_id();
        //执行查询业务
        List<User> userList=dutyService.queryUserByRoleId(roleid);
        //结果集返回给新的jsp
        request.setAttribute("userList",userList);
        request.setAttribute("MSG","修改成功!!");
        //跳转页面
        return "People_List";
    }
    @RequestMapping(value = "search.do",method = RequestMethod.POST)
    public String search(String value,String type,HttpServletRequest request){
        //获取登陆者
        User users=(User)request.getSession().getAttribute("user");
        int roleid=users.getRole_id();
        //创建user对象
        User user1=new User();
        user1.setRole_id(roleid);
        List<User>userList_AllRoleNname=dutyService.queryByAllRoleNname(user1);
        switch (type){
            case "姓名":
                user1.setName(value);
                List<User>userList_name=dutyService.queryByName(user1);
                request.setAttribute("userList",userList_name);
                break;
            case "职务":
                user1.setRole_name(value);
                List<User>userList_role_name=dutyService.queryByRole_name(user1);
                request.setAttribute("userList",userList_role_name);
                break;
            case "mail":
                user1.setMail(value);
                List<User>userList_mail=dutyService.queryByMail(user1);
                request.setAttribute("userList",userList_mail);
                break;
            case "age":
                user1.setAge(value);
                List<User>userList_age=dutyService.queryByAge(user1);
                request.setAttribute("userList",userList_age);
                break;
            default:
                request.setAttribute("error","没有找到符合条件的数据");
                break;
        }
        return "People_List";
    }
}
